package fr.curriculumvitae.anthony.stephan.fragments;

import fr.curriculumvitae.anthony.stephan.R;
import fr.curriculumvitae.anthony.stephan.SwipeyTabsActivity;
import fr.curriculumvitae.anthony.stephan.pinnedsections.PinnedSectionListView;
import fr.curriculumvitae.anthony.stephan.pinnedsections.PinnedSectionListView.PinnedSectionListAdapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class ProductionsFragment extends ListFragment {
	
	private static Fragment INSTANCE = null;
	
	private static SwipeyTabsActivity fragmentController;
	
	public static final int SECTION_DIAG			= 0;
	public static final int SECTION_OTA	 			= 1;
	public static final int SECTION_UPMCONTACT 		= 2;
	public static final int SECTION_PICTURESEDITOR 	= 3;
	public static final int SECTION_AVC 			= 4;
	public static final int SECTION_MANDELBROT		= 5;
	public static final int SECTION_REGULATOR		= 6;
	public static final int SECTION_DISTRIBUTOR		= 7;
	public static final int SECTION_SUPRA			= 8;
	public static final int SECTION_CLEANER			= 9;
	
	private static String[] sectionLabels;
	private static String[] productions;
	private static String[] establishments;
	private static String[] dates;
	private static String[] resumes;
	private static String[] links;
	private static String[] linksDisplay;
	
	private static int firstDisplayedProduction = 0;
	private static boolean scrollAtDisplay = false;
	
	public static Fragment getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new ProductionsFragment();
		} 
		return INSTANCE;
	}
	
	public static void displayProduction(int productionID) {
		firstDisplayedProduction = productionID;
		scrollAtDisplay = true;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.pinned_head_list, null);
		return root;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		fragmentController = (SwipeyTabsActivity) getActivity();
 		inflateArrays();
		initializeAdapter();
		initializeListView();
	}
 	
	private void initializeListView() {
	    getListView().setPadding(0, 0, 0, 0);
	    ((PinnedSectionListView)getListView()).setShadowVisible(false);
	    if(scrollAtDisplay) {
	    	scrollToProduction(firstDisplayedProduction);
	    	firstDisplayedProduction = 0;
	    	scrollAtDisplay = false;
	    }
	}

    private void inflateArrays() {
    	sectionLabels = getResources().getStringArray(R.array.productions_sections_labels);
    	productions = getResources().getStringArray(R.array.productions_productions_labels);
    	establishments = getResources().getStringArray(R.array.productions_establishments);
    	dates = getResources().getStringArray(R.array.productions_dates);
    	resumes = getResources().getStringArray(R.array.productions_resumes);
    	links = getResources().getStringArray(R.array.productions_links);
    	linksDisplay = getResources().getStringArray(R.array.productions_links_display);
    }

    private void initializeAdapter() {
    	SimpleAdapter adapter = new SimpleAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1);
    	setListAdapter(adapter);
    }
    
    private void scrollToProduction(final int sectionID) {
	    ((PinnedSectionListView)getListView()).post(new Runnable() {
			@Override
	    	public void run() {
	    		((PinnedSectionListView)getListView()).setSelection(2*sectionID);
	    	}
	    });
    }
    
    
      /////////////
     // ADAPTER //
    /////////////
    
	private static class SimpleAdapter extends ArrayAdapter<Production> implements PinnedSectionListAdapter {

        public SimpleAdapter(Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);

            final int sectionsNumber = sectionLabels.length;
            prepareSections(sectionsNumber);
            
            int sectionPosition = 0, listPosition = 0;
            
            for(char i=0; i<sectionsNumber; i++) {
                Production section = new Production(Production.SECTION, sectionLabels[i], null, null, null, null, null, null);
                section.sectionNumber = i;
                section.sectionPosition = sectionPosition;
                section.listPosition = listPosition++;
                onSectionAdded(section, sectionPosition);
                add(section);
                
                Production item = new Production(Production.ITEM, sectionLabels[i], productions[i], 
                							 establishments[i], dates[i], links[i], linksDisplay[i], resumes[i]);
                item.sectionNumber = i;
                item.sectionPosition = sectionPosition;
                item.listPosition = listPosition++;
                add(item);
                sectionPosition++;
            }
        }

        protected void prepareSections(int sectionsNumber) {
        	/* nothing to do */
        }
        
        protected void onSectionAdded(Production section, int sectionPosition) { 
        	/* nothing to do */
        }

        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {
        	LayoutInflater inflater = (LayoutInflater) fragmentController.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	View view = null;
        	Production item = getItem(position);
        	if(item.type == Production.SECTION) {
        		view = inflater.inflate(R.layout.layout_header, null);
                ((TextView) view).setText(item.label);
        		switch(item.sectionNumber) {
        			case SECTION_DIAG:
        				view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_green));
        				break;
	        		case SECTION_OTA:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_blue));
	        			break;
	        		case SECTION_UPMCONTACT:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_violet));
	        			break;
	        		case SECTION_PICTURESEDITOR:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_red));
	        			break;
	        		case SECTION_AVC:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_orange));
	        			break;
	        		case SECTION_MANDELBROT:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_green));
	        			break;
	        		case SECTION_REGULATOR:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_blue));
	        			break;
	        		case SECTION_DISTRIBUTOR:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_violet));
	        			break;
	        		case SECTION_SUPRA:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_red));
	        			break;
	        		case SECTION_CLEANER:
	        			view.setBackgroundColor(parent.getResources().getColor(R.color.pinnedheader_head_orange));
	        			break;
        		}
        		view.setTag(null);
        		
        	} else if(item.type == Production.ITEM) {
        		view = inflater.inflate(R.layout.layout_row_production, null);
        		TextView tvProduction = (TextView) view.findViewById(R.id.textView_production_label);
        		TextView tvEstablishment = (TextView) view.findViewById(R.id.textView_production_establishment);
        		TextView tvDates = (TextView) view.findViewById(R.id.textView_production_dates);
        		TextView tvLink = (TextView) view.findViewById(R.id.textView_production_link);
        		TextView tvSpoiler = (TextView) view.findViewById(R.id.textView_resume_spoiler_text);
        		TextView tvResume = (TextView) view.findViewById(R.id.textView_production_resume);
        		ImageView ivLogo = (ImageView) view.findViewById(R.id.imageView_headband);
        		ImageView ivSpoilerArrow = (ImageView) view.findViewById(R.id.imageView_resume_spoiler_arrow);
        		ImageView ivPhoto = (ImageView) view.findViewById(R.id.imageView_production_photo);
        		
        		tvProduction.setText(item.production);
        		tvEstablishment.setText(item.establishment);
        		tvDates.setText(item.dates);
        		tvResume.setText(item.resume);
        		tvLink.setMovementMethod(LinkMovementMethod.getInstance());
        		tvLink.setText(Html.fromHtml("<a href=\'"+item.link+"\'>"+item.linkDisplay+"</a>"));
        		
        		switch(item.sectionNumber) {
        			case SECTION_DIAG:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_alcatellucent));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_diag));
	        			break;
	        		case SECTION_OTA:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_alcatellucent_blue));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_ota));
	        			break;
	        		case SECTION_UPMCONTACT:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_upmc_violet));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_upmcontact));
	        			break;
	        		case SECTION_PICTURESEDITOR:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_upmc_red));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_pictureseditor));
	        			break;
	        		case SECTION_AVC:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_3soleils_orange));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_avc));
	        			break;
	        		case SECTION_MANDELBROT:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_upmc));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_mandelbrot));
	        			break;
	        		case SECTION_REGULATOR:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_lps_blue));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_regulator));
	        			break;
	        		case SECTION_DISTRIBUTOR:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_lps_violet));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_distributor));
	        			break;
	        		case SECTION_SUPRA:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_supra_red));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_supra));
	        			break;
	        		case SECTION_CLEANER:
	        			ivLogo.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_headband_lycee_orange));
	        			ivPhoto.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.im_photo_cleaner));
	        			break;
        		}
        		
        		tvSpoiler.setText(fragmentController.getResources().getString(R.string.display_production_resume));
        		ivSpoilerArrow.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_arrow_to_right));
        		tvSpoiler.setOnClickListener(new OnResumeSpoilerClickListener(ivSpoilerArrow, tvSpoiler, tvResume));
        	}
        	
            return view;
        }

        @Override 
        public int getViewTypeCount() {
            return 2;
        }

        @Override 
        public int getItemViewType(int position) {
            return getItem(position).type;
        }

        @Override
        public boolean isItemViewTypePinned(int viewType) {
            return viewType == Production.SECTION;
        }
    }

	@SuppressWarnings("unused")
	private static class Production {
		public static final int ITEM = 0;
		public static final int SECTION = 1;
		public int sectionNumber;
		public int sectionPosition;
		public int listPosition;
		
		public final int type;
		public final String label;
		public final String production;
		public final String establishment;
		public final String dates;
		public final String link;
		public final String linkDisplay;
		public final String resume;
		
		public Production(int type, String label, 
				String production, String establishment, String dates, String link, String linkDisplay, String resume) {
		    this.type = type;
		    this.label = label;
		    this.production = production;
		    this.establishment = establishment;
		    this.dates = dates;
		    this.link = link;
		    this.linkDisplay = linkDisplay;
		    this.resume = resume;
		}
		@Override 
		public String toString() {
			return label;
		}
	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
	
	private static class OnResumeSpoilerClickListener implements OnClickListener {
		private final ImageView arrowView;
		private final TextView spoilerView;
		private final TextView resumeView;
		public OnResumeSpoilerClickListener(ImageView arrowView, TextView spoilerView, TextView resumeView) {
			this.arrowView = arrowView;
			this.spoilerView = spoilerView;
			this.resumeView = resumeView;
		}
		@Override
		public void onClick(View v) {
			if(resumeView.getVisibility() == View.GONE) {
				spoilerView.setText(fragmentController.getResources().getString(R.string.hide_production_resume));
				arrowView.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_arrow_to_bottom));
				resumeView.setVisibility(View.VISIBLE);
			} else {
				spoilerView.setText(fragmentController.getResources().getString(R.string.display_production_resume));
				arrowView.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_arrow_to_right));
				resumeView.setVisibility(View.GONE);
			}
		}
	};
	
}
